package com.workflow.workflowproject.config;

import com.workflow.workflowproject.listener.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.RuntimeService;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.transaction.PlatformTransactionManager;

@Slf4j
@AllArgsConstructor
@Configuration
public class FlowableConfig implements ApplicationListener<ContextRefreshedEvent>{
    @Autowired
    private SpringProcessEngineConfiguration configuration;
    //流程结束监听
    @Autowired
    private EndFlowListener endFlowListener;
    //流程开始监听
    private final StartFlowListener startFlowListener;
    //任务开始监听
    @Autowired
    private TaskCreateListener taskCreateListener;
    @Autowired
    private TaskCompletedListener taskCompletedListener;
    //创建定时作业监听
    @Autowired
    private TimeFlowListener timeFlowListener;
    //作业执行失败
    @Autowired
    private DeleteFlowListener deleteFlowListener;
    //作业执行失败
    @Autowired
    private ErrorFlowListener errorFlowListener;

    @Bean
    public ProcessEngineConfiguration processEngineConfiguration(SqlSessionFactory sqlSessionFactory, PlatformTransactionManager annotationDrivenTransactionManager) {
        SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
        // 指定 MyBatis-Flex  数据源
        processEngineConfiguration.setDataSource(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource());
        // 配置 MyBatis-Flex  的事务管理器
        processEngineConfiguration.setTransactionManager(annotationDrivenTransactionManager);
        return processEngineConfiguration;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        log.info("————————————进入流程配置——————————————开始");
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        log.info("》》》》：注入终止事件进行监听");
        //流程结束全局监听(达到终止事件进行监听)
        dispatcher.addEventListener(endFlowListener, FlowableEngineEventType.PROCESS_COMPLETED);
        dispatcher.addEventListener(endFlowListener, FlowableEngineEventType.PROCESS_COMPLETED_WITH_TERMINATE_END_EVENT);
        log.info("》》》》：注入流程删除时进行监听");
        //流程删除时进行监听 PROCESS_CANCELLED
        dispatcher.addEventListener(deleteFlowListener, FlowableEngineEventType.PROCESS_CANCELLED);
        log.info("》》》》：注入流程发起时进行监听");
        //流程发起时进行监听  PROCESS_STARTED
        dispatcher.addEventListener(startFlowListener, FlowableEngineEventType.PROCESS_STARTED);
        log.info("》》》》：注入任务创建时发起监听");
        //任务创建时发起监听  TASK_CREATED
        dispatcher.addEventListener(taskCreateListener, FlowableEngineEventType.TASK_CREATED);
        //任务已经完成时发起监听  TASK_COMPLETED
        log.info("》》》》：注入任务已经完成时发起监听");
        dispatcher.addEventListener(taskCompletedListener, FlowableEngineEventType.TASK_COMPLETED);
        log.info("》》》》：注入创建定时作业监听");
        //创建定时作业监听 TIMER_SCHEDULED
        dispatcher.addEventListener(timeFlowListener, FlowableEngineEventType.TIMER_SCHEDULED);
        log.info("》》》》：注入作业执行失败监听");
        //作业执行失败。该作业与异常包含在本事件里 JOB_EXECUTION_FAILURE
        dispatcher.addEventListener(errorFlowListener, FlowableEngineEventType.JOB_EXECUTION_FAILURE);
        log.info("————————————进入流程配置——————————————结束");
    }

}